#include <stdio.h>
#include <stdlib.h>

#define ERROR (1e7)
typedef enum {push, pop, end} Operation; /* 操作类型 */
typedef enum {false, true} bool;
typedef int SElemSet;    
typedef int Position; // 元素下标

struct StackNode {
    int capacity;         /* 两个顺序栈的总容量 */
    Position top1, top2;  /* 两个顺序栈的栈顶指针 */
    SElemSet *data;       /* 存储数据的数组 */
};
typedef struct StackNode *Stack;

void InitStack (Stack stack, int n){
    stack->capacity = n;
    stack->top1 = -1;
    stack->top2 = n;
    stack->data = (SElemSet*)malloc(sizeof(SElemSet)*n);
}

bool Push (Stack stack, int tag, SElemSet x){
    if(stack->top1 + 1  == stack->top2){
        printf("Stack Full\n");
        return false;
    }
    if(tag == 1){
        stack->top1++;
        stack->data[stack->top1] = x;
        return true;
    }else{
        stack->top2--;
        stack->data[stack->top2] = x;
        return true;
    }
}
bool IsEmpty(Stack stack, int tag){
    if(tag == 1){
        if(stack->top1 == -1) return true;
    }else{
        if(stack->top2 == stack->capacity) return true;
    }
}
SElemSet TopPop (Stack stack, int tag){
    if(IsEmpty(stack, tag)){
        return ERROR;
    }
    SElemSet ans;
    if(tag == 1){
        ans = stack->data[stack->top1];
        stack->top1--;
    }else if(tag == 2){
        ans = stack->data[stack->top2];
        stack->top2++;
    }
    return ans;
}

Operation GetOp ()
{ /* 判断输入的操作类型 */
    char op[5]; /* 操作最长4个字符 */
    Operation ret; 
    
    scanf(" %s ", op);
    switch (op[1]) { /* 用第2个字母区分操作 */
        case 'u': ret = push; break;
        case 'o': ret = pop; break;
        case 'n': ret = end; break;
        default: break;
    }
    return ret;
}

void PrintStack (Stack stack, int tag)
{
    printf("Pop from Stack %d:", tag);
    while (!IsEmpty(stack, tag)) {
        printf(" %d", TopPop(stack, tag));
    }
    printf("\n");
}
int main(void)
{
    int n, tag, x;
    Stack stack;
    bool done;
    stack = (Stack)malloc(sizeof(struct StackNode));
    scanf("%d", &n); // 数组长度为n
    InitStack(stack, n); 
    done = false; 
    while ( done == false ) {
        switch( GetOp() ) {
            case push: 
                scanf("%d %d\n", &tag, &x);
                if (Push(stack, tag, x) == false) {
                    printf("%d is not in Stack %d\n", x, tag);
                }
                break;
            case pop: 
                scanf("%d\n", &tag);
                x = TopPop(stack, tag);
                if (x == ERROR) {
                    printf("Stack %d is Empty\n", tag);
                }
                break;
            case end: 
                PrintStack(stack, 1);
                PrintStack(stack, 2);
                done = true;
                break;
            default: break;
        }
    }
    return 0;
}